import request from '@/utils/request'
import type { ApiResponse } from '@/types/api'

// 生成任务状态
export type TaskStatus = 'PENDING' | 'RUNNING' | 'COMPLETED' | 'FAILED' | 'CANCELLED'

// 生成任务
export interface GenerateTask {
  taskId: string
  name: string
  type: string
  status: TaskStatus
  progress: number
  startTime?: string
  endTime?: string
  message?: string
  result?: any
}

// 生成配置
export interface GenerateConfig {
  staticPath: string
  staticUrlPrefix: string
  fileExtension: string
  batchSize: number
  options: string[]
}

// 统计信息
export interface StatisticsInfo {
  totalFiles: number
  totalSize: number
  totalSizeFormatted: string
  lastGenerated: string
  fileTypeStats: Record<string, number>
}

// 生成管理API
export const generateApi = {
  // 获取生成配置
  getConfig(siteId: number): Promise<ApiResponse<GenerateConfig>> {
    return request({
      url: `/admin/generate/config/${siteId}`,
      method: 'get',
    })
  },

  // 保存生成配置
  saveConfig(siteId: number, config: GenerateConfig): Promise<ApiResponse<void>> {
    return request({
      url: `/admin/generate/config/${siteId}`,
      method: 'post',
      data: config,
    })
  },

  // 验证生成配置
  validateConfig(config: GenerateConfig): Promise<ApiResponse<void>> {
    return request({
      url: '/admin/generate/config/validate',
      method: 'post',
      data: config,
    })
  },

  // 获取任务列表
  getTasks(): Promise<ApiResponse<GenerateTask[]>> {
    return request({
      url: '/admin/generate/tasks',
      method: 'get',
    })
  },

  // 获取任务详情
  getTask(taskId: string): Promise<ApiResponse<GenerateTask>> {
    return request({
      url: `/admin/generate/task/${taskId}`,
      method: 'get',
    })
  },

  // 取消任务
  cancelTask(taskId: string): Promise<ApiResponse<boolean>> {
    return request({
      url: `/admin/generate/tasks/${taskId}/cancel`,
      method: 'post',
    })
  },

  // 删除任务
  deleteTask(taskId: string): Promise<ApiResponse<void>> {
    return request({
      url: `/admin/generate/task/${taskId}`,
      method: 'delete',
    })
  },

  // 清理已完成的任务
  cleanupTasks(): Promise<ApiResponse<void>> {
    return request({
      url: '/admin/generate/tasks/cleanup',
      method: 'delete',
    })
  },

  // 生成全站
  generateAll(options?: any): Promise<ApiResponse<GenerateTask>> {
    return request({
      url: '/admin/generate/all',
      method: 'post',
      data: options,
    })
  },

  // 生成指定站点
  generateSite(siteId: number, options?: any): Promise<ApiResponse<GenerateTask>> {
    return request({
      url: `/admin/generate/site/${siteId}`,
      method: 'post',
      data: options,
    })
  },

  // 生成首页
  generateIndex(siteId: number): Promise<ApiResponse<GenerateTask>> {
    return request({
      url: `/admin/generate/index/${siteId}`,
      method: 'post',
    })
  },

  // 生成栏目
  generateChannels(siteId: number, channelIds: number[]): Promise<ApiResponse<GenerateTask>> {
    return request({
      url: `/admin/generate/channels/${siteId}`,
      method: 'post',
      data: { channelIds },
    })
  },

  // 生成文章
  generateArticles(siteId: number, articleIds: number[]): Promise<ApiResponse<GenerateTask>> {
    return request({
      url: `/admin/generate/articles/${siteId}`,
      method: 'post',
      data: { articleIds },
    })
  },

  // 生成站点地图
  generateSitemap(siteId: number): Promise<ApiResponse<GenerateTask>> {
    return request({
      url: `/admin/generate/sitemap/${siteId}`,
      method: 'post',
    })
  },

  // 清空静态文件
  clearStatic(siteId: number, options?: string[]): Promise<ApiResponse<void>> {
    return request({
      url: `/admin/generate/clear/${siteId}`,
      method: 'post',
      data: { options },
    })
  },

  // 获取统计信息
  getStatistics(siteId: number): Promise<ApiResponse<StatisticsInfo>> {
    return request({
      url: `/admin/generate/statistics/${siteId}`,
      method: 'get',
    })
  },

  // 清理静态文件
  clearStaticFiles(options: string[]): Promise<ApiResponse<void>> {
    return request({
      url: '/admin/generate/clear-files',
      method: 'post',
      data: { options },
    })
  },

  // 导出统计报告
  exportStatisticsReport(siteId?: number): Promise<Blob> {
    return request({
      url: '/admin/generate/export-statistics',
      method: 'get',
      params: { siteId },
      responseType: 'blob'
    })
  },

  // 刷新缓存
  refreshCache(siteId: number): Promise<ApiResponse<void>> {
    return request({
      url: `/admin/generate/cache/refresh/${siteId}`,
      method: 'post',
    })
  },

  // 预览生成结果
  previewGenerate(siteId: number, type: string, id?: number): Promise<ApiResponse<string>> {
    return request({
      url: `/admin/generate/preview/${siteId}`,
      method: 'get',
      params: { type, id },
    })
  },

  // 获取生成进度
  getProgress(taskId: string): Promise<ApiResponse<{ progress: number; message: string }>> {
    return request({
      url: `/admin/generate/progress/${taskId}`,
      method: 'get',
    })
  },

  // 导出统计报告
  exportStatistics(siteId: number): Promise<ApiResponse<string>> {
    return request({
      url: `/admin/generate/export/${siteId}`,
      method: 'get',
    })
  },
}
